from django import forms
from django.contrib.auth.models import User

class RegistrationForm(forms.Form):
    username = forms.RegexField(
        label= "Username",
        max_length= 30,
        regex= r'^\w+$',
        help_text = "Required. Your QUEST User ID.",
        error_message= "This value must contain only letters, numbers and underscores."
    )

    student_id = forms.RegexField(
        label = "Student ID",
        max_length = 8,
        regex = r'^\d{8}$',
        help_text = "Required. Your 8 digit UW Student ID number.",
        error_message= "This value must be an 8-digit student number."
    )

    password1 = forms.CharField(
        label="Password",
        widget=forms.PasswordInput
    )

    password2 = forms.CharField(
        label="Confirm password",
        widget=forms.PasswordInput
    )

    first_name = forms.CharField(
        label = "First name",
        max_length=80,
    )

    last_name = forms.CharField(
        label = "Last name",
        max_length=80,
    )

    expected_graduation = forms.IntegerField(
        label="Expected graduation",
        min_value = 2009,
        max_value = 3001,
        help_text = "Required. The year of your expected graduation.",
    )

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError("A user with that username already exists.")

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1", "")
        password2 = self.cleaned_data["password2"]
        if password1 != password2:
            raise forms.ValidationError("The two password fields didn't match.")
        return password2

class ProfileForm(forms.Form):
    email = forms.EmailField(
        label = "Email Address",
    )

    student_id = forms.RegexField(
        label = "Student ID",
        max_length = 8,
        regex = r'^\d{8}$',
        help_text = "Required. Your 8 digit UW Student ID number.",
        error_message= "This value must be an 8-digit student number."
    )

    # password1 = forms.CharField(
    #     label="Password",
    #     widget=forms.PasswordInput
    # )

    # password2 = forms.CharField(
    #     label="Confirm password",
    #     widget=forms.PasswordInput
    # )

    first_name = forms.CharField(
        label = "First name",
        max_length=80,
    )

    last_name = forms.CharField(
        label = "Last name",
        max_length=80,
    )

    expected_graduation = forms.IntegerField(
        label="Required. The year of your expected graduation.",
        min_value = 2009,
        max_value = 3001,
        help_text = "Year",
    )

    #def clean_password2(self):
    #    password1 = self.cleaned_data.get("password1", "")
    #    password2 = self.cleaned_data["password2"]
    #    if password1 != password2:
    #        raise forms.ValidationError("The two password fields didn't match.")
    #    return password2

